#include "mainwindow.h"
#include "./ui_mainwindow.h"


MainWindow::MainWindow(QWidget* parent)
    :   QMainWindow(parent)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    progressBar = new QProgressBar();
    progressBar->setRange(0, 100);
    // progressBar->setValue(100);
    layout->addWidget(progressBar);

    QPushButton *startButton = new QPushButton("开始", this);
    layout->addWidget(startButton);

    connect(startButton, &QPushButton::clicked, this, &MainWindow::startTask);
    connect(worker, &Worker::progressChanged, progressBar, &QProgressBar::setValue);
    // 创建一个中心部件并将布局设置为其中心部件的布局
    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
}

MainWindow::~MainWindow(){
    delete ui;
}

void MainWindow::startTask() {
    // worker.doWork();
    // 启动一个新线程执行耗时任务
    QThread *thread = new QThread;
    worker->moveToThread(thread);

    connect(thread, &QThread::started, worker, &Worker::doWork);
    // connect(&worker, &Worker::finished, thread, &QThread::quit);
    // connect(&worker, &Worker::finished, &worker, &Worker::deleteLater);
    connect(thread, &QThread::finished, thread, &QThread::deleteLater);
    connect(thread, &QThread::finished, progressBar, &QProgressBar::reset);
    connect(thread, &QThread::finished, [](){
        qDebug()<<"123";
    });

    thread->start();
}


